/*
 * 版权所有 (C) 2013 长沙市红耀通信息技术有限公司。保留所有权利。
 *
 * http://www.zhiqim.com/fadfox/ 欢迎加盟[知凡狐]兴趣小组。
 *
 * 创建说明： 2013-6-29 由 zhichenggang创建
 */
package net.redxyz.repost.service.task.repost;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import net.redxyz.repost.service.dao.RepostDao;
import net.redxyz.repost.service.dao.UserDao;
import net.redxyz.top.dbo.user.User;
import net.redxyz.top.dbo.user.UserGift;
import net.redxyz.top.repost.dbo.AssRepostPlan;

import org.zhiqim.kernel.logging.Log;
import org.zhiqim.kernel.logging.LogFactory;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.kernel.schedule.Task;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Selector;

/**
 * 每天对上架进行更新任务 Day(5, 37, 19);
 * 
 * @version v1.0.0 @author zouzhigang 2014-3-21 新建与整理
 */
public class RepostPlanChkTask implements Task
{
    private static final Log log = LogFactory.getLog("timer.repost.log");

    public void execute()
    {
        try
        {
            System.gc();

            List<AssRepostPlan> planList = new ArrayList<AssRepostPlan>();
            Selector selector = new Selector();
            selector.addOrderbyAsc("nick");
            selector.addOrderbyAsc("createTime");

            int count = ORM.table().count(AssRepostPlan.class, selector);
            for (int i = 0; i < (count - 1) / 10000 + 1; i++)
            {
                PageResult<AssRepostPlan> page = ORM.table().page(AssRepostPlan.class, (i + 1), 10000, selector);
                planList.addAll(page.list());
            }

            HashMap<String, ArrayList<AssRepostPlan>> nickMap = new HashMap<String, ArrayList<AssRepostPlan>>();
            for (AssRepostPlan plan : planList)
            {
                ArrayList<AssRepostPlan> list = nickMap.get(plan.getNick());
                if (list == null)
                {
                    list = new ArrayList<AssRepostPlan>();
                    nickMap.put(plan.getNick(), list);
                }

                list.add(plan);
            }

            log.info("均衡上架检查共" + nickMap.size() + "个用户");

            String datetime = DateTimes.getDateTimeString();
            for (Iterator<Entry<String, ArrayList<AssRepostPlan>>> it = nickMap.entrySet().iterator(); it.hasNext();)
            {
                Entry<String, ArrayList<AssRepostPlan>> entry = it.next();
                String nick = entry.getKey();

                User user = UserDao.getUser(nick);
                if (user.getUserStatus() != 0)
                {
                    UserDao.doCloseUser(user);
                    continue;
                }

                if (user.getVersionNo() <= 1)
                {// 用户到期或免费用户
                    Selector sel = new Selector("nick", nick);
                    sel.addMaybe("status", 1);
                    sel.addMaybeThenG("versionNo", 1);
                    sel.addMaybeThenG("endDate", datetime.substring(0, 10));
                    UserGift userGift = ORM.get(ZTable.class).item(UserGift.class, sel);
                    if (userGift == null)
                    {// 用户未赠送，关闭用户
                        UserDao.doCloseUser(user);
                        continue;
                    }
                }

//                if (!UserDao.isAccredit(user))
//                {
//                    log.info("宝贝同步任务,用户授权到期[" + user.getNick() + "]");
//                    continue;
//                }

                ArrayList<AssRepostPlan> list = entry.getValue();
                RepostDao.doChkRepostPlan(log, user.getNick(), user.getSessionId(), datetime, list);
            }
        }
        catch (Exception e)
        {
            log.error("宝贝同步任务异常", e);
        }
    }

}
